package com.maiworld.seller.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.maiworld.pojo.TbUser;
import com.maiworld.seller.service.UserService;

import entity.PageResult;
import entity.Result;

@RestController
@RequestMapping("/user")
public class UserController {

	@Reference
	private UserService userService;
	
	@RequestMapping("/findAll")
	public List<TbUser> findAll(){
		return userService.findAll();
	}
	
	@RequestMapping("/findPage")
	public PageResult findPage(int page , int rows){
		return userService.findPage(page, rows);
	}
	
	@RequestMapping("/add")
	public Result add( @RequestBody TbUser user){
		try{
			userService.add(user);
			return new Result(true, "增加成功");
		}catch(Exception e){
			e.printStackTrace();
			return new Result(false, "增加失败");
		}
	}
	
	@RequestMapping("/update")
	public Result update(@RequestBody TbUser user){
		try{
			userService.update(user);
			return new Result(true, "修改成功");
		}catch(Exception e){
			e.printStackTrace();
			return new Result(false, "修改失败");
		}		
	}
	
	@RequestMapping("/findOne")	
	public TbUser findOne(Long id){
		return userService.findOne(id);
	}
	
	@RequestMapping("/delete")
	public Result delete(Long [] ids){
		try{
			userService.delete(ids);
			return new Result(true, "删除成功");
		}catch(Exception e){
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
		
	}
	
	/**
	 * 查询+分页
	 * @param 
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/search")
	public PageResult search(@RequestBody TbUser user, int page, int rows  ){
		return userService.findPageUser(user, page, rows);		
	}
	
	/**
	 * 用户管理
	 */
	/*@RequestMapping("/login")
	public String login(TbUser user){
		return "login.html";
	}*/
		
	//登录验证
	@RequestMapping("/login1")
	public Map<String, Object>  login(@RequestParam("username") String username,
						 @RequestParam("password") String password
				){
		try {
			username = new String((username).trim().getBytes("ISO-8859-1"), "UTF-8");
		   } catch (UnsupportedEncodingException e) {    
		    e.printStackTrace();
		   } 
		System.out.print(username);
		Map<String, Object> result= new HashMap<String, Object>();
		TbUser user = new TbUser();
		user.setUsername(username);
		user.setPassword(password);
		TbUser userinfo = userService.login(user);
		if(userinfo !=null){
			userinfo.setPassword("******");
			result.put("user", userinfo);
			result.put("statusCode", 200);
			return result;
		}else {
			result.put("statusCode", 400);
			return result;
		}
	}
		
//	@RequestMapping("/login1")
//	public String  login(@RequestParam("username") String username,
//						 @RequestParam("password") String password,
//						 Model model
//				){
//		TbUser user = new TbUser();
//		user.setUsername(username);
//		user.setPassword(password);
//		if(userService.login(user) !=null){
//			model.addAttribute("username",username);
//			return "redirect:/index.html";
//		}else {
//			model.addAttribute("error","账号密码错误");
//			return "redirect:/login.html";
//		}
//	}
 
	
	
	
}
